package com.car.loan.project.cd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.OperatorStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.DataScope;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cd.domain.CdCredit;
import com.car.loan.project.cd.domain.CdCreditAlbum;
import com.car.loan.project.cd.domain.bo.CreditApplyInfoAddBo;
import com.car.loan.project.cd.domain.bo.CreditApplyInfoUpdateBo;
import com.car.loan.project.cd.domain.bo.CreditInputInfoUpdateBo;
import com.car.loan.project.cd.domain.select.CreditBaseSelect;
import com.car.loan.project.cd.domain.vo.CreditAlbumVo;
import com.car.loan.project.cd.domain.vo.CreditBaseVo;
import com.car.loan.project.cd.mapper.CdCreditAlbumMapper;
import com.car.loan.project.cd.mapper.CdCreditMapper;
import com.car.loan.project.cd.service.ICdCreditService;
import com.car.loan.project.cd.util.enums.CreditAlbumType;
import com.car.loan.project.cd.util.enums.CreditStatus;
import com.car.loan.project.system.service.ISysSequenceService;
import com.car.loan.project.ve.domain.VeVehicleAlbum;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 征信Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-28
 */
@Service
public class CdCreditServiceImpl implements ICdCreditService {

    @Autowired
    private CdCreditMapper cdCreditMapper;

    @Autowired
    private ISysSequenceService sequenceCreditServiceImpl;

    @Autowired
    private CdCreditAlbumMapper creditAlbumMapper;

    /**
     * 查询征信
     *
     * @param id 征信ID
     * @return 征信
     */
    @Override
    public CdCredit selectCdCreditById(Long id) {
        return cdCreditMapper.selectById(id);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public Page<CreditBaseVo> findCreditBasePage(CreditBaseSelect creditBaseSelect) {
        Page<CreditBaseVo> page = new Page<>(creditBaseSelect.getPageNum(),
            creditBaseSelect.pageSize);
        return cdCreditMapper.findCreditBasePage(page, creditBaseSelect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CdCredit save(CreditApplyInfoAddBo creditApplyInfoAddBo) {
        //添加 征信业务逻辑信息
        Long loginUserId = SecurityUtils.getLoginUserId();
        CdCredit cdCredit = new CdCredit();
        BeanUtils.copyBeanProp(cdCredit, creditApplyInfoAddBo);
        cdCredit.setNo(sequenceCreditServiceImpl.next());
        Integer operatorStatus = creditApplyInfoAddBo.getOperatorStatus();
        cdCredit.setStatus(CreditStatus.INIT.status);
        if (OperatorStatus.SAVE.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.INIT.status);
        } else if (OperatorStatus.SUBMIT.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.SUBMIT.status);
            cdCredit.setSubmitTime(LocalDateTime.now());
            cdCredit.setSubmitAdminId(loginUserId);
        }

        cdCredit.setCreatorId(loginUserId);
        cdCredit.setOperatorId(loginUserId);
        cdCredit.setOperateTime(LocalDateTime.now());
        cdCredit.setCreateTime(LocalDateTime.now());
        cdCreditMapper.insert(cdCredit);
        //添加图片视频信息
        List<String> images = creditApplyInfoAddBo.getImages();
        if (StringUtils.isNotEmpty(images)) {
            for (String image : images) {
                CdCreditAlbum cdCreditAlbum = new CdCreditAlbum();
                cdCreditAlbum.setCreditId(cdCredit.getId());
                cdCreditAlbum.setType(CreditAlbumType.IMAGE.type);
                cdCreditAlbum.setUrl(image);
                cdCreditAlbum.setCreatorId(loginUserId);
                creditAlbumMapper.insert(cdCreditAlbum);
            }

        }
        List<String> videos = creditApplyInfoAddBo.getVideos();
        if (StringUtils.isNotEmpty(videos)) {
            for (String video : videos) {
                CdCreditAlbum cdCreditAlbum = new CdCreditAlbum();
                cdCreditAlbum.setCreditId(cdCredit.getId());
                cdCreditAlbum.setType(CreditAlbumType.VIDEO.type);
                cdCreditAlbum.setUrl(video);
                cdCreditAlbum.setCreatorId(loginUserId);
                creditAlbumMapper.insert(cdCreditAlbum);
            }
        }
        return cdCredit;
    }

    @Override
    public AjaxResult update(CreditApplyInfoUpdateBo creditApplyInfoUpdateBo) {
        //判断当前状态
        CdCredit cdCredit = cdCreditMapper.selectById(creditApplyInfoUpdateBo.getId());
        if (null == cdCredit) {
            return AjaxResult.error("无该征信记录");
        }
        if (!(CreditStatus.INIT.status == cdCredit.getStatus()
            || CreditStatus.REVOKE.status == cdCredit.getStatus())) {
            return AjaxResult.error("已提交无法编辑");
        }
        Long loginUserId = SecurityUtils.getLoginUserId();
        BeanUtils.copyBeanProp(cdCredit, creditApplyInfoUpdateBo);
        cdCredit.setOperateTime(LocalDateTime.now());
        cdCredit.setOperatorId(loginUserId);
        Integer operatorStatus = creditApplyInfoUpdateBo.getOperatorStatus();
        if (OperatorStatus.SAVE.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.INIT.status);
        } else if (OperatorStatus.SUBMIT.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.SUBMIT.status);
            cdCredit.setSubmitTime(LocalDateTime.now());
            cdCredit.setSubmitAdminId(loginUserId);
        } else {
            return AjaxResult.error("无该操作状态");
        }
        cdCreditMapper.updateById(cdCredit);
        //添加图片视频信息
        List<CreditAlbumVo> images = creditApplyInfoUpdateBo.getImages();

        if (StringUtils.isNotEmpty(images)) {
            for (CreditAlbumVo image : images) {
                if (null == image.getId() || image.getId() == 0) {
                    CdCreditAlbum cdCreditAlbum = new CdCreditAlbum();
                    cdCreditAlbum.setCreditId(cdCredit.getId());
                    cdCreditAlbum.setType(CreditAlbumType.IMAGE.type);
                    cdCreditAlbum.setUrl(image.getUrl());
                    cdCreditAlbum.setCreatorId(loginUserId);
                    creditAlbumMapper.insert(cdCreditAlbum);
                }
            }
        }
        List<CreditAlbumVo> videos = creditApplyInfoUpdateBo.getVideos();
        if (StringUtils.isNotEmpty(videos)) {
            for (CreditAlbumVo video : videos) {
                if (null == video.getId() || video.getId() == 0) {
                    CdCreditAlbum cdCreditAlbum = new CdCreditAlbum();
                    cdCreditAlbum.setCreditId(cdCredit.getId());
                    cdCreditAlbum.setType(CreditAlbumType.VIDEO.type);
                    cdCreditAlbum.setUrl(video.getUrl());
                    cdCreditAlbum.setCreatorId(loginUserId);
                    creditAlbumMapper.insert(cdCreditAlbum);
                }
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult input(CreditInputInfoUpdateBo creditInputInfoUpdateBo) {

        //判断当前状态
        CdCredit cdCredit = cdCreditMapper.selectById(creditInputInfoUpdateBo.getId());
        if (null == cdCredit) {
            return AjaxResult.error("无该征信记录");
        }
        if (CreditStatus.SUBMIT.status != cdCredit.getStatus()) {
            return AjaxResult.error("已录入无法编辑");
        }
        Long loginUserId = SecurityUtils.getLoginUserId();
        Integer operatorStatus = creditInputInfoUpdateBo.getOperatorStatus();
        if (OperatorStatus.SAVE.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.SUBMIT.status);
        } else if (OperatorStatus.SUBMIT.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.COMPLETE.status);
            cdCredit.setInputTime(LocalDateTime.now());
            cdCredit.setInputAdminId(loginUserId);
        } else if (OperatorStatus.REVOKE.status == operatorStatus) {
            cdCredit.setStatus(CreditStatus.REVOKE.status);
            cdCredit.setInputTime(LocalDateTime.now());
            cdCredit.setInputAdminId(loginUserId);
        } else {
            return AjaxResult.error("无该操作状态");
        }
        cdCredit.setRemark(creditInputInfoUpdateBo.getRemark());
        cdCredit.setQueryDate(creditInputInfoUpdateBo.getQueryDate());
        cdCredit.setDetail(creditInputInfoUpdateBo.getDetail());
        cdCredit.setOperateTime(LocalDateTime.now());
        cdCredit.setOperatorId(SecurityUtils.getLoginUserId());
        cdCreditMapper.updateById(cdCredit);

        //添加图片信息
        List<CreditAlbumVo> images = creditInputInfoUpdateBo.getImages();
        if (StringUtils.isNotEmpty(images)) {
            for (CreditAlbumVo image : images) {
                CdCreditAlbum cdCreditAlbum = new CdCreditAlbum();
                cdCreditAlbum.setCreditId(cdCredit.getId());
                cdCreditAlbum.setType(CreditAlbumType.IMAGE.type);
                cdCreditAlbum.setUrl(image.getDbUrl());
                cdCreditAlbum.setCreatorId(loginUserId);
                creditAlbumMapper.insert(cdCreditAlbum);
            }
        }

        return AjaxResult.success();
    }

    /**
     * 查询征信列表
     *
     * @param cdCredit 征信
     * @return 征信
     */
    @Override
    public List<CdCredit> selectCdCreditList(CdCredit cdCredit) {
        QueryWrapper<CdCredit> queryWrapper = new QueryWrapper<>(cdCredit);
        return cdCreditMapper.selectList(queryWrapper);
    }


    @Override
    public List<CdCredit> selectCdCreditQueryList(QueryWrapper<CdCredit> queryWrapper) {
        return cdCreditMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CdCredit> selectPage(IPage<CdCredit> page, QueryWrapper<CdCredit> queryWrapper) {
        return cdCreditMapper.selectPage(page, queryWrapper);
    }

    /**
     * 新增征信
     *
     * @param cdCredit 征信
     * @return 结果
     */
    @Override
    public int insertCdCredit(CdCredit cdCredit) {

        return cdCreditMapper.insert(cdCredit);
    }

    /**
     * 修改征信
     *
     * @param cdCredit 征信
     * @return 结果
     */
    @Override
    public int updateCdCredit(CdCredit cdCredit) {

        return cdCreditMapper.updateById(cdCredit);
    }

    /**
     * 删除征信对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCdCreditByIds(Collection<Long> ids) {
        return cdCreditMapper.deleteBatchIds(ids);
    }

    /**
     * 删除征信信息
     *
     * @param id 征信ID
     * @return 结果
     */
    @Override
    public int deleteCdCreditById(Long id) {
        return cdCreditMapper.deleteById(id);
    }
}
